package com.zenith.api.controller;

import com.zenith.api.feign.user.UserServiceFeignClient;
import com.zenith.common.result.PageResult;
import com.zenith.common.result.Result;
import com.zenith.api.po.dto.user.*;
import com.zenith.api.po.vo.user.PermissionVO;
import com.zenith.api.po.vo.user.RoleVO;
import com.zenith.api.po.vo.user.UserVO;
import com.zenith.api.po.vo.user.RolePermissionVO;
import com.zenith.api.po.vo.user.UserRoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 用户控制器 - API服务
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户管理接口")
@RequiredArgsConstructor
public class UserController {
    
    private final UserServiceFeignClient userServiceFeignClient;
    
    /**
     * 检查用户是否是活跃状态
     */
    @Operation(summary = "检查用户是否是活跃状态")
    @PostMapping("/getUserActive")
    public Result<Boolean> getUserActive(@RequestParam("userId") Long userId) {
        return userServiceFeignClient.getUserActive(userId);
    }

    /**
     * 获取当前用户信息接口
     */
    @Operation(summary = "获取当前用户信息")
    @PostMapping("/getCurrentUser")
    public Result<Object> getCurrentUser(@RequestParam("userId") Long userId) {
        return userServiceFeignClient.getCurrentUser(userId);
    }

    // =========================== 用户管理相关接口 ===========================
    
    /**
     * 创建用户
     */
    @Operation(summary = "创建用户", description = "创建新的用户账户")
    @PostMapping("/createUser")
    public Result<String> createUser(@RequestBody UserDTO userDTO) {
        return userServiceFeignClient.createUser(userDTO);
    }
    
    /**
     * 更新用户
     */
    @Operation(summary = "更新用户", description = "更新用户信息")
    @PostMapping("/updateUser")
    public Result<String> updateUser(@RequestBody UserDTO userDTO) {
        return userServiceFeignClient.updateUser(userDTO);
    }
    
    /**
     * 删除用户
     */
    @Operation(summary = "删除用户", description = "逻辑删除指定的用户")
    @PostMapping("/deleteUser/{id}")
    public Result<String> deleteUser(@PathVariable Long id) {
        return userServiceFeignClient.deleteUser(id);
    }
    
    /**
     * 获取用户详情
     */
    @Operation(summary = "获取用户详情", description = "根据ID查询用户详细信息")
    @PostMapping("/getUserById/{id}")
    public Result<UserVO> getUserById(@PathVariable Long id) {
        return userServiceFeignClient.getUserById(id);
    }
    
    /**
     * 按用户名查询
     */
    @Operation(summary = "按用户名查询", description = "根据用户名查询用户信息")
    @PostMapping("/getUserByUsername")
    public Result<UserVO> getUserByUsername(@RequestParam String username) {
        return userServiceFeignClient.getUserByUsername(username);
    }
    
    /**
     * 分页查询用户
     */
    @Operation(summary = "分页查询用户", description = "分页查询用户列表信息")
    @PostMapping("/getUserPage")
    public Result<PageResult<UserVO>> getUserPage(@RequestBody UserDTO query) {
        return userServiceFeignClient.getUserPage(query);
    }
    
    /**
     * 批量查询用户
     */
    @Operation(summary = "批量查询用户", description = "根据用户ID列表批量查询用户信息")
    @PostMapping("/getUsersByIds")
    public Result<List<UserVO>> getUsersByIds(@RequestBody List<Long> ids) {
        return userServiceFeignClient.getUsersByIds(ids);
    }
    
    /**
     * 更新用户状态
     */
    @Operation(summary = "更新用户状态", description = "启用或禁用用户账户")
    @PostMapping("/updateUserStatus/{id}")
    public Result<String> updateUserStatus(@PathVariable Long id, @RequestParam Integer status) {
        return userServiceFeignClient.updateUserStatus(id, status);
    }
    
    /**
     * 重置密码
     */
    @Operation(summary = "重置密码", description = "重置指定用户的登录密码")
    @PostMapping("/resetPassword/{id}")
    public Result<String> resetPassword(@PathVariable Long id, @RequestParam String newPassword) {
        return userServiceFeignClient.resetPassword(id, newPassword);
    }
    
    /**
     * 获取当前用户信息（新接口）
     */
    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户的详细信息")
    @PostMapping("/getCurrentUserInfo")
    public Result<UserVO> getCurrentUserInfo(@RequestParam Long userId) {
        return userServiceFeignClient.getCurrentUserInfo(userId);
    }
    
    /**
     * 更新个人资料
     */
    @Operation(summary = "更新个人资料", description = "更新当前用户的个人资料")
    @PostMapping("/updateProfile")
    public Result<String> updateProfile(@RequestBody UserDTO userDTO) {
        return userServiceFeignClient.updateProfile(userDTO);
    }
    
    /**
     * 修改密码
     */
    @Operation(summary = "修改密码", description = "修改用户登录密码")
    @PostMapping("/changePassword")
    public Result<String> changePassword(@RequestParam Long userId, @RequestParam String oldPassword, @RequestParam String newPassword) {
        return userServiceFeignClient.changePassword(userId, oldPassword, newPassword);
    }
    
    /**
     * 上传头像
     */
    @Operation(summary = "上传头像", description = "上传用户头像")
    @PostMapping("/uploadAvatar")
    public Result<String> uploadAvatar(@RequestParam Long userId, @RequestParam String avatarUrl) {
        return userServiceFeignClient.uploadAvatar(userId, avatarUrl);
    }
    
    /**
     * 批量分配用户角色
     */
    @Operation(summary = "批量分配用户角色", description = "为多个用户批量分配角色")
    @PostMapping("/batchAssignRoles")
    public Result<String> batchAssignRoles(@RequestParam List<Long> userIds, @RequestParam List<String> roleIds) {
        return userServiceFeignClient.batchAssignRoles(userIds, roleIds);
    }
    
    /**
     * 批量删除用户（旧接口）
     */
    @Operation(summary = "批量删除用户（旧接口）")
    @PostMapping("/deleteUsers")
    public Result<Void> deleteUsers(@RequestBody List<String> ids) {
        return userServiceFeignClient.deleteUsers(ids);
    }
    
    /**
     * 高级搜索用户
     */
    @Operation(summary = "高级搜索用户", description = "按多条件搜索用户")
    @PostMapping("/searchUsers")
    public Result<PageResult<UserVO>> searchUsers(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String role,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return userServiceFeignClient.searchUsers(keyword, status, role, pageNum, pageSize);
    }

    // =========================== 权限管理相关接口 ===========================
    
    @Operation(summary = "根据ID查询权限")
    @PostMapping("/getPermissionById")
    public Result<PermissionVO> getPermissionById(@RequestParam String id) {
        return userServiceFeignClient.getPermissionById(id);
    }

    @Operation(summary = "获取权限列表")
    @PostMapping("/getPermissionList")
    public Result<List<PermissionVO>> getPermissionList() {
        return userServiceFeignClient.getPermissionList();
    }

    @Operation(summary = "分页查询权限")
    @PostMapping("/getPermissionPage")
    public Result<PageResult<PermissionVO>> getPermissionPage(@RequestBody PermissionDTO query) {
        return userServiceFeignClient.getPermissionPage(query);
    }

    @Operation(summary = "判断用户的角色是否拥有此权限")
    @GetMapping("/check")
    public Result<Boolean> getPermissionIds(@RequestParam Long userId, @RequestParam String requiredPermission) {
        return userServiceFeignClient.getPermissionIds(userId, requiredPermission);
    }

    @Operation(summary = "根据角色获取权限列表")
    @PostMapping("/selectPermissionsByRole")
    public Result<List<PermissionVO>> selectPermissionsByRole(@RequestParam("role") String role) {
        return userServiceFeignClient.selectPermissionsByRole(role);
    }

    // =========================== 角色管理相关接口 ===========================
    
    @Operation(summary = "根据ID查询角色")
    @PostMapping("/getRoleById")
    public Result<RoleVO> getRoleById(@RequestParam String id) {
        return userServiceFeignClient.getRoleById(id);
    }

    @Operation(summary = "获取角色列表")
    @PostMapping("/getRoleList")
    public Result<List<RoleVO>> getRoleList() {
        return userServiceFeignClient.getRoleList();
    }

    @Operation(summary = "分页查询角色")
    @PostMapping("/getRolePage")
    public Result<PageResult<RoleVO>> getRolePage(@RequestBody RoleDTO query) {
        return userServiceFeignClient.getRolePage(query);
    }

    @Operation(summary = "更新角色")
    @PostMapping("/updateRole")
    public Result<Void> updateRole(@RequestBody RoleVO entity) {
        return userServiceFeignClient.updateRole(entity);
    }

    // =========================== 角色权限关联管理相关接口 ===========================
    
    @Operation(summary = "根据ID查询角色权限关联")
    @PostMapping("/getRolePermissionById")
    public Result<RolePermissionVO> getRolePermissionById(@RequestParam String id) {
        return userServiceFeignClient.getRolePermissionById(id);
    }

    @Operation(summary = "获取角色权限关联列表")
    @PostMapping("/getRolePermissionList")
    public Result<List<RolePermissionVO>> getRolePermissionList() {
        return userServiceFeignClient.getRolePermissionList();
    }

    @Operation(summary = "分页查询角色权限关联")
    @PostMapping("/getRolePermissionPage")
    public Result<PageResult<RolePermissionVO>> getRolePermissionPage(@RequestBody RolePermissionDTO query) {
        return userServiceFeignClient.getRolePermissionPage(query);
    }

    // =========================== 用户角色关联管理相关接口 ===========================
    
    @Operation(summary = "根据ID查询用户角色关联")
    @PostMapping("/getUserRoleById")
    public Result<UserRoleVO> getUserRoleById(@RequestParam String id) {
        return userServiceFeignClient.getUserRoleById(id);
    }

    @Operation(summary = "获取用户角色关联列表")
    @PostMapping("/getUserRoleList")
    public Result<List<UserRoleVO>> getUserRoleList() {
        return userServiceFeignClient.getUserRoleList();
    }

    @Operation(summary = "分页查询用户角色关联")
    @PostMapping("/getUserRolePage")
    public Result<PageResult<UserRoleVO>> getUserRolePage(@RequestBody UserRoleDTO query) {
        return userServiceFeignClient.getUserRolePage(query);
    }

    @Operation(summary = "获取用户角色ID列表")
    @PostMapping("/getUserRoleIds")
    public Result<Set<String>> getUserRoleIds(@RequestParam("userId") Long userId) {
        return userServiceFeignClient.getUserRoleIds(userId);
    }

    @Operation(summary = "获取用户权限ID列表")
    @PostMapping("/getUserPermissionIds")
    public Result<Set<String>> getUserPermissionIds(@RequestParam("userId") Long userId) {
        return userServiceFeignClient.getUserPermissionIds(userId);
    }
}